package com.qumitech.visualnovel.model.common;

import com.alibaba.fastjson.*;
import java.io.Serializable;
import java.util.List;

public class BaseResponse<T> extends JSONObject implements Serializable{

    public BaseResponse() {
        this.put("code", 0);
        this.put("msg", "success");
        this.put("data", new JSONObject());
    }

    public BaseResponse(int code, String msg) {
        this.put("code", code);
        this.put("msg", msg);
        this.put("data", new JSONObject());
    }

    public BaseResponse(Object data) {
        this.put("code", 0);
        this.put("msg", "success");
        this.put("data", JSONObject.parseObject(JSON.toJSONString(data)));
    }

    public BaseResponse(int code, String msg, Object data) {
        this.put("code", code);
        this.put("msg", msg);
        this.put("data", JSONObject.parseObject(JSON.toJSONString(data)));
    }

    public BaseResponse(JSONObject data) {
        this.put("code", 0);
        this.put("msg", "success");
        this.put("data", data);
    }

    public BaseResponse(int code, String msg, JSONObject data) {
        this.put("code", code);
        this.put("msg", msg);
        this.put("data", data);
    }

    public BaseResponse(JSONArray data) {
        this.put("code", 0);
        this.put("msg", "success");
        this.put("total", data.size());
        this.put("data", data);
    }

    public BaseResponse(JSONArray data, int total) {
        this.put("code", 0);
        this.put("msg", "success");
        this.put("total", total);
        this.put("data", data);
    }

    public BaseResponse(int code, String msg, JSONArray data) {
        this.put("code", code);
        this.put("msg", msg);
        this.put("total", data.size());
        this.put("data", data);
    }

    public BaseResponse(int code, String msg, JSONArray data, int total) {
        this.put("code", code);
        this.put("msg", msg);
        this.put("total", total);
        this.put("data", data);
    }

    public BaseResponse(List<T> list){
        this.put("code", 0);
        this.put("msg", "success");
        this.put("total", list.size());
        this.put("data", new JSONArray());
        list.forEach(x->{
            this.getJSONArray("data").add(JSONObject.parseObject(JSON.toJSONString(x)));
        });
    }

    public BaseResponse(List<T> list, int total){
        this.put("code", 0);
        this.put("msg", "success");
        this.put("total", total);
        this.put("data", new JSONArray());
        list.forEach(x->{
            this.getJSONArray("data").add(JSONObject.parseObject(JSON.toJSONString(x)));
        });
    }

    public static BaseResponse fromServerResponse(String response) {
        JSONObject jResult = JSONObject.parseObject(response);
        if (jResult.containsKey("error")) {
            JSONObject jError = jResult.getJSONObject("error");
            int code = jError.getInteger("status").intValue();
            String msg = jError.getString("description");
            return new BaseResponse(code, msg);
        } else if (jResult.containsKey("message") && jResult.containsKey("status")) {
            int code = jResult.getInteger("status").intValue();
            String msg = jResult.getString("message");
            JSONObject result = jResult.getJSONObject("data");
            return new BaseResponse(code, msg, result);
        }
        return new BaseResponse();
    }
}
